package com.siemens.industrial.utils;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

/**
 * Created by qupen on 2016/11/18.
 */
public class ExportExcel {
    public static String exportExcel(List<Map<String, Object>> sheetData, String name) throws IOException {
        if (sheetData.size() == 0)
            return "";
        HSSFWorkbook workbook = new HSSFWorkbook();//excel 文件
        HSSFSheet sheet = workbook.createSheet(name);//当前工作表
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局：居中
        cellStyle.setWrapText(true);
        List<String> keyList = new ArrayList<>();

        getSheetsData(sheetData, sheet, keyList, cellStyle);

        String path = ConfigurationUtil.INSTANCE.getExcelPath();
        String filename = UUIDGenerator.getUUID();
        FileOutputStream out = new FileOutputStream(path + filename);
        workbook.write(out);
        out.close();

        return filename;
    }

    public static String exportExcel(List<Map<String, Object>> sheets) throws IOException {
        if (sheets.size() == 0)
            return "";

        HSSFWorkbook workbook = new HSSFWorkbook();//excel 文件
        for (Map<String, Object> sheetMap : sheets) {
            HSSFSheet sheet = workbook.createSheet(sheetMap.get("mtid").toString());//当前工作表
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平布局：居中
            cellStyle.setWrapText(true);
            List<String> keyList = new ArrayList<>();
            List<Map<String, Object>> sheetData = (List<Map<String, Object>>) sheetMap.get("values");
            if (sheetData.size() > 0) {
                getSheetsData(sheetData, sheet, keyList, cellStyle);
            }
        }

        String path = ConfigurationUtil.INSTANCE.getExcelPath();
        String name = UUIDGenerator.getUUID();
        FileOutputStream out = new FileOutputStream(path + name);
        workbook.write(out);
        out.close();

        return name;
    }

    private static void getSheetsData(List<Map<String, Object>> sheetData, HSSFSheet sheet, List<String> keyList, HSSFCellStyle cellStyle) {
        for (int i = 0; i < sheetData.size() + 1; i++) {
            HSSFRow row = sheet.createRow(i);//创建一行
            if (i == 0) {
                Set<String> d = sheetData.get(0).keySet();
                Iterator<String> it = d.iterator();
                while (it.hasNext()) {
                    keyList.add(it.next());
                }
                for (int j = 0; j < keyList.size(); j++) {
                    HSSFCell cell = row.createCell(j);
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setCellStyle(cellStyle);
                    cell.setCellValue(keyList.get(j));
                }
            } else {
                for (int z = 0; z < sheetData.get(i - 1).size(); z++) {
                    HSSFCell cell = row.createCell(z);
                    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    cell.setCellStyle(cellStyle);
                    if (sheetData.get(i - 1).get(keyList.get(z)) == null) {
                        cell.setCellValue("null");
                    } else {
                        cell.setCellValue(sheetData.get(i - 1).get(keyList.get(z)).toString());
                    }
                }
            }
        }
    }
}
